package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrdersMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class OrderTask {
    @Autowired
    OrdersMapper ordersMapper;

    @Scheduled(cron = "0 * * * * ?")
    public void processTimeoutOrder() {
        //获取当前时间
        LocalDateTime time = LocalDateTime.now().minusMinutes(15);

        List<Orders> order = ordersMapper.findByStatusAndOrdertimeLT(Orders.PENDING_PAYMENT, time);
        if (order != null && order.size() > 0) {
            order.forEach(orders -> {
                orders.setStatus(Orders.CANCELLED);
                orders.setCancelTime(LocalDateTime.now());
                orders.setCancelReason("支付超时，自动取消");
                ordersMapper.update(orders);
            });
        }
    }
}
